package com.sz.admin.browsehistory.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.browsehistory.service.BrowseHistoryService;
import com.sz.admin.browsehistory.pojo.po.BrowseHistory;
import com.sz.admin.browsehistory.mapper.BrowseHistoryMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.browsehistory.pojo.dto.BrowseHistoryCreateDTO;
import com.sz.admin.browsehistory.pojo.dto.BrowseHistoryUpdateDTO;
import com.sz.admin.browsehistory.pojo.dto.BrowseHistoryListDTO;
import com.sz.admin.browsehistory.pojo.dto.BrowseHistoryImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.browsehistory.pojo.vo.BrowseHistoryVO;

/**
 * <p>
 * 用户浏览历史 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-06-30
 */
@Service
@RequiredArgsConstructor
public class BrowseHistoryServiceImpl extends ServiceImpl<BrowseHistoryMapper, BrowseHistory> implements BrowseHistoryService {
    @Override
    public void create(BrowseHistoryCreateDTO dto){
        BrowseHistory browseHistory = BeanCopyUtils.copy(dto, BrowseHistory.class);
        long count;
        save(browseHistory);
    }

    @Override
    public void update(BrowseHistoryUpdateDTO dto){
        BrowseHistory browseHistory = BeanCopyUtils.copy(dto, BrowseHistory.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(BrowseHistory::getHistoryId, dto.getHistoryId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        // 唯一性校验
        long count;
        count = QueryChain.of(BrowseHistory.class).eq(BrowseHistory::getHistoryId, dto.getHistoryId()).ne(BrowseHistory::getHistoryId, dto.getHistoryId()).count();
        CommonResponseEnum.EXISTS.message("historyId已存在").assertTrue(count > 0);
        saveOrUpdate(browseHistory);
    }

    @Override
    public PageResult<BrowseHistoryVO> page(BrowseHistoryListDTO dto){
        Page<BrowseHistoryVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), BrowseHistoryVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<BrowseHistoryVO> list(BrowseHistoryListDTO dto){
        return listAs(buildQueryWrapper(dto), BrowseHistoryVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public BrowseHistoryVO detail(Object id){
        BrowseHistory browseHistory = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(browseHistory);
        return BeanCopyUtils.copy(browseHistory, BrowseHistoryVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<BrowseHistoryImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), BrowseHistoryImportDTO.class, true);
        List<BrowseHistoryImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(BrowseHistoryListDTO dto, HttpServletResponse response) {
        List<BrowseHistoryVO> list = list(dto);
        String fileName = "用户浏览历史模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "用户浏览历史", BrowseHistoryVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(BrowseHistoryListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(BrowseHistory.class);
        if (Utils.isNotNull(dto.getUserId())) {
            wrapper.eq(BrowseHistory::getUserId, dto.getUserId());
        }
        if (Utils.isNotNull(dto.getPostId())) {
            wrapper.like(BrowseHistory::getPostId, dto.getPostId());
        }
        return wrapper;
    }
}